//
//  HRBaseViewController.m
//  haierapp
//
//  Created by Xia Zhiyong on 14-8-22.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRBaseViewController.h"

@interface HRBaseViewController ()

@end

@implementation HRBaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    self.navigationItem.backBarButtonItem = backItem;
    backItem.title = @"";
    [self.view setBackgroundColor:[UIColor colorFromHexRGB:@"f0f0f0"]];
    if (__CUR_IOS_VERSION > __IPHONE_7_0) {
        [self.view setTintColor:kAppActiveColor];
        backItem.title = @"";
    }else{
        backItem.title = @"返回";
    }
    
    //页面加载耗时
    [self analysisStart];
    
    _progressHUD=[HRProgressHUD ProgressHUDWithType:HUDTYPE_LOADING];
    _progressHUD.delegate=self;
    
    _isFirstLoadForHUD=YES;//是第一次加载
}

- (void)viewDidAppear:(BOOL)animated
{
    _isViewShow = YES;
    [super viewWillAppear:animated];
   
}

- (void)viewWillDisappear:(BOOL)animated
{
    _isViewShow = NO;
    [super viewWillDisappear:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}

- (void)alertMsg:(NSString *)msg
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}


- (void)toast:(NSString *)str
{
    if(str == nil || [str isEqualToString:@""])
    {
        return;
    }
    PGToast *toast = [PGToast makeToast:str];
    [toast show];
}

- (void)isNeedHidenTabbar:(BOOL)isNeed
{
    //isNeed = NO;//高保真不需隐藏工具栏
    float originY = isNeed ? 480:431;
    
    [UIView beginAnimations:nil context:NULL] ;
    [UIView setAnimationDuration:0.3];
    for(UIView *view in self.tabBarController.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            [view setFrame:CGRectMake(view.frame.origin.x, originY, view.frame.size.width, view.frame.size.height)];
        }
        else
        {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width,originY)];
        }
    }
    [UIView commitAnimations];
}

#pragma mark - 页面加载耗时

//页面加载耗时开始
-(void)analysisStart{
    _flagFirstLoad=YES;
    _startTime=[[NSDate date] timeIntervalSince1970];
}

//页面加载耗时结束
-(void)analysisEnd:(NSString *)pageTitle{
    
    if (_flagFirstLoad) {
        double currentTime=[[NSDate date] timeIntervalSince1970];
        
        double duration=currentTime - _startTime;
        duration=duration*1000;//以毫秒为单位
        
        NSString *moduleId=self.title;
        
        if (pageTitle!=nil && ![pageTitle isEqualToString:@""]) {
            moduleId=pageTitle;
        }
        
        moduleId = [moduleId stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        //加载页面耗时
        //[uAnalysisManager onActivityResumeEvent:duration withModuleId:moduleId];
        
        _flagFirstLoad=NO;//不是第一次加载
    }
}


#pragma mark - 数据加载提示

//显示加载提示
-(void)showCustomLoadingHud:(NSString *)text inView:(UIView *)view{
    if (_isFirstLoadForHUD) {
        [self showHudByType:HUDTYPE_LOADING];
        _isFirstLoadForHUD=NO;//标示不是第一次加载
    }else{
        [self showLoadingHud:text];
    }
}

//隐藏加载提示
-(void)hideCustomLoadingHud{
    [self hideHud];
    [self hideLoadingHud];
}


/**
 * @brief 显示提示页面
 **/
-(void)showHudByType:(HUDTYPE)type{
    [_progressHUD showHUDInView:self.view withType:type];
}

/**
 * @brief 隐藏提示页面
 **/
-(void)hideHud{
    [_progressHUD hideHUD];
}


#pragma mark - HRProgressHud Delegate Methods
-(void)tapHUDButtonAction:(id)button{
    
}

@end
